package com.gitee.zhanyao.security.config;

import com.gitee.zhanyao.security.jwt.JwtTokenEnhancer;
import com.gitee.zhanyao.security.jwt.OauthJwtAccessTokenConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * token jwt 存储配置
 * @author zhanyao
 */
@Configuration
public class JwtTokenConfig {

    @Bean
    public TokenStore jwtTokenStore(JwtAccessTokenConverter converter) {
        return new JwtTokenStore(converter);
    }

    /**
     * 生成 jwt 令牌
     *
     * @return
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(UserDetailsService userDetailsService) {
        JwtAccessTokenConverter accessTokenConverter = new OauthJwtAccessTokenConverter(userDetailsService);
        // 密钥
        accessTokenConverter.setSigningKey("zhanyao");
        return accessTokenConverter;
    }

    @Bean
    public TokenEnhancer jwtTokenEnhancer() {
        return new JwtTokenEnhancer();
    }
}


